package com.ruoyi.inspection.utils;

import com.ruoyi.common.utils.sign.Md5Utils;

public class ShortUrlUtils {
    /**
     * 生成思路：
     1.将"原始链接（长链接）+key(自定义字符串,防止算法泄漏)"MD5加密
     2.把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算，把得到的值与 0x0000003D 进行位与运算，取得字符数组 chars 索引，
     把取得的字符相加，每次循环按位右移 5 位，把字符串存入对应索引的输出数组(4组6位字符串)
     3.生成4以下的随机数，从输入数组中取出随机数对应位置的字符串，作为短链，存入数据库或者NoSql
     解析方式
     编写一个web处理程序,把从ur(如:http://url.51bi.com/zAnuAn)中解析短链接,将解析到的短链接(zAnuAn)与数据库中存入的原始链接进行匹配，
     跳转到匹配到的原始链接
     * @param string
     * @return
     */
    public static String[] ShortText(String string){
        String key = "feixiang";                 //自定义生成MD5加密字符串前的混合KEY
        String[] chars = new String[]{          //要使用生成URL的字符
                "a","b","c","d","e","f","g","h",
                "i","j","k","l","m","n","o","p",
                "q","r","s","t","u","v","w","x",
                "y","z","0","1","2","3","4","5",
                "6","7","8","9","A","B","C","D",
                "E","F","G","H","I","J","K","L",
                "M","N","O","P","Q","R","S","T",
                "U","V","W","X","Y","Z"
        };
        String hex = Md5Utils.hash(key + string);
        int hexLen = hex.length();
        int subHexLen = hexLen / 8;
        String[] ShortStr = new String[4];

        for (int i = 0; i < subHexLen; i++) {
            String outChars = "";
            // 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算
            String sTempSubString = hex.substring(i * 8, i * 8 + 8);
            // 这里需要使用 long 型来转换，因为 Inteper .parseInt() 只能处理 31 位 , 首位为符号位 , 如果不用long ，则会越界
            long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16);
            for (int j = 0; j < 6; j++) {
                // 把得到的值与 0x0000003D 进行位与运算，取得字符数组 chars 索引
                long index = 0x0000003D & lHexLong;
                // 把取得的字符相加
                outChars += chars[(int) index];
                // 每次循环按位右移 5 位
                lHexLong = lHexLong >> 5;
            }
            // 把字符串存入对应索引的输出数组
            ShortStr[i] = outChars;
        }
        return ShortStr;
    }

    public static String ShortText(String string, int index){
        if(index > 3) index = 3;
        String linkUrl = string;
        String[] linkUrls = ShortUrlUtils.ShortText(linkUrl);
        StringBuilder ss = new StringBuilder();
        for(String s : linkUrls){
            ss.append(s);
        }
        ss.append(string);
        ss.append(IdFactory.getNewId());
        linkUrls = ShortUrlUtils.ShortText(ss.toString());
        if(linkUrls.length >= index) {
            linkUrl = linkUrls[index];
        }else{
            linkUrl = linkUrls[0];
        }
        return linkUrl;
    }
    public static String ShortTextOne(String string){
        int index = 0;
        String linkUrl = string;
        String[] linkUrls = ShortUrlUtils.ShortText(linkUrl);
        StringBuilder ss = new StringBuilder();
        for(String s : linkUrls){
            ss.append(s);
        }
        ss.append(string);
        ss.append(IdFactory.getNewId());
        linkUrls = ShortUrlUtils.ShortText(ss.toString());
        if(linkUrls.length >= index) {
            linkUrl = linkUrls[index];
        }else{
            linkUrl = linkUrls[0];
        }
        return linkUrl;
    }

    public static String ShortTextOne(){
        String str = IdFactory.getNewId() + "";
        return ShortTextOne(str);
    }
}
